/**
 * GenerateControl.java [V 1.0.0]
 * classes : com.xiaocq.wordsgod.GenerateControl
 * xiaocq Create at 2012-7-12 上午10:39:36
 */
package com.xiaocq.wordsgod;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xiaocq.wordsgod.SlideButton.OnSlideListener;
import com.xiaocq.wordsgod.db.WordSetUtil;
import com.xiaocq.wordsgod.model.Control;

/**
 * com.xiaocq.wordsgod.GenerateControl
 * @author Mystory
 * Create at 2012-7-12 上午10:39:36
 */
public class GenerateControl extends Activity implements OnClickListener{
	/*用于填充类型对话框的内容*/
	private static final String[] TYPE_ITEMS = new String[]{
		"所有单词",
		"未测单词",
		"已测单词",
		"答对单词",
		"答错单词"
	};
	
	private static final int NUM_DIALOG = 1;
	private static final int UNIT_DIALOG = 2;
	private static final int DIY_DIALOG = 3;
	
	/*标题*/
	private LinearLayout navigator;
	private Button backToMenu, loadControl;
	private TextView title;
	/*选项*/
	private SlideButton mModel, mType;
	private Button mRange, mNum, mStart; 
	
	private SharedPreferences pref;
	
	private WordSetUtil wordSetUtil;
	
	private String[] nums;	//保存数量的数组
	
	private int mTestId,mTotalNum;	//对应Control中的相应选项
	private boolean mTestType;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.generatecontrol);
		
		initComponent();
		
		pref = getSharedPreferences(Control.PREF_NAME, Context.MODE_PRIVATE);
		//检查进度，是否有存档
		checkProcess(pref);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(wordSetUtil != null){
			wordSetUtil.close();
			wordSetUtil = null;
		}
	}
	
	//初始化
	private void initComponent(){
		navigator = (LinearLayout) findViewById(R.id.navigator);
		
		backToMenu = (Button) navigator.findViewById(R.id.nav_leftBtn);
		title = (TextView) navigator.findViewById(R.id.nav_middleText);
		loadControl = (Button) navigator.findViewById(R.id.nav_rightBtn);
		
		loadControl.setEnabled(false);
		
		backToMenu.setText(R.string.backToMenu);
		title.setText(R.string.wordTest);
		loadControl.setText(R.string.load);
		
		mModel = (SlideButton) findViewById(R.id.gc_testId);
		mType = (SlideButton) findViewById(R.id.gc_testType);
		
		/*设置图片资源，必须要有的*/
		mModel.setImageResource(R.drawable.model_bg, R.drawable.slide);
		mType.setImageResource(R.drawable.type_bg, R.drawable.slide);
		
		mRange = (Button) findViewById(R.id.gc_testId_change);
		mNum = (Button) findViewById(R.id.gc_testNum);
		
		mStart = (Button) findViewById(R.id.gc_gameStart);
		
		/*初次启动的默认值*/
		mModel.setSwitchOn(true);
		mType.setSwitchOn(false);
		
		//将默认的值显示出来
		setRangeAndNum(1);
		
		backToMenu.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		loadControl.setOnClickListener( new OnClickListener() {
			public void onClick(View v) {
				Control control = Control.loadFromXml(pref);
				System.out.println(control.toString());
				Intent intent = control.convertToIntent();
				intent.setClass(GenerateControl.this, WordTest.class);
				startActivity(intent);
			}
		});
		
		mModel.setOnSlideListener(new OnSlideListener() {
			public void onSlideCompleted(boolean isSwitchOn) {
				mTestType = isSwitchOn;
			}
		});
		
		mType.setOnSlideListener(new OnSlideListener() {
			public void onSlideCompleted(boolean isSwitchOn) {
				setRangeAndNum(isSwitchOn ? 51 : 1);
			}
		});
		
		mRange.setOnClickListener(this);
		mNum.setOnClickListener(this);
		
		mStart.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				/*数量为0不能进行测试*/
				if(mTotalNum == 0){
					Toast.makeText(GenerateControl.this, R.string.testNumZero, 1500).show();
					return;
				}
				Control control = Control.newInstance(mTestId, mTestType, mTotalNum);
				Intent intent = control.convertToIntent();
				intent.setClass(GenerateControl.this, WordTest.class);
				startActivity(intent);
			}
		});
	}
	
	private void checkProcess(SharedPreferences pref){
		boolean isNewTest = Control.isNewTest(pref);
		if(!isNewTest){
			loadControl.setEnabled(true);
			Toast.makeText(GenerateControl.this, R.string.hasSavedTest, 1000).show();
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = null;
		switch (id) {
		case NUM_DIALOG:
			builder = new AlertDialog.Builder(GenerateControl.this);
			builder.setItems(nums, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					/*改变数量*/
					mTotalNum = Integer.valueOf(nums[which]);
					mNum.setText(mTotalNum + "个");
				}
			});
			return builder.create();
		case UNIT_DIALOG:
			builder = new AlertDialog.Builder(GenerateControl.this);
			builder.setItems(R.array.unit_list, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					/*单元测试*/
					mTestId = which + 1;
					setRangeAndNum(mTestId);
				}
			});
			return builder.create();
		case DIY_DIALOG:
			builder = new AlertDialog.Builder(GenerateControl.this);
			builder.setItems(TYPE_ITEMS, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					/*自主测试*/
					mTestId = which + 51;
					setRangeAndNum(mTestId);
				}
			});
			return builder.create();
		default:
			break;
		}
		return super.onCreateDialog(id);
	}
	
	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		/*数量是动态变化的，所以强调数量选择对话框必须是动态建立的*/
		if(id == NUM_DIALOG){
			removeDialog(id);
		}
	}
	
	/*显示指定的内容*/
	private void setRangeAndNum(int testId){
		generateTestNum(testId);
		mNum.setText(nums[0] + "个");
		if(testId <= 50){
			mRange.setText("Unit " + testId);
		}else{
			mRange.setText(TYPE_ITEMS[testId - 51]);
		}
		
		mTestId = testId;
		mTestType = mModel.isSwitchOn();
		mTotalNum = Integer.valueOf(nums[0]);
	}
	
	/*根据选择的测试类型，生成对应的数量数组*/
	private void generateTestNum(int testId){
		if(wordSetUtil == null){
			wordSetUtil = new WordSetUtil(GenerateControl.this);
			wordSetUtil.open();
		}
		int num = wordSetUtil.getNumByTestId(testId);
		int index = 1;
		int size;
		
		if(num <= 40){
			size = 1;
		}else if(num % 20 == 0){
			size = num / 20 - 1;
		}else {
			size = num / 20;
		}
		
		size = size > 5 ? 5 : size;
		
		nums = new String[size];
		nums[0] = Integer.toString(num);
		for(int i=100; i>=40; i-=20){
			if(i < num && (i != 0)){
				nums[index++] = Integer.toString(i);
			}
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.gc_testId_change:
			if(mType.isSwitchOn()){
				showDialog(DIY_DIALOG);
			}else{
				showDialog(UNIT_DIALOG);
			}
			break;
		case R.id.gc_testNum:
			showDialog(NUM_DIALOG);
		default:
			break;
		}
	}
}
